import { defHttp } from '/@/utils/http/axios';
import { Modal } from 'ant-design-vue';
import { useGlobSetting } from '/@/hooks/setting';
import { UploadFileParams } from '/#/axios';
import { stringFormat } from '/@/utils/common/compUtils';
const globSetting = useGlobSetting();

enum Api {
  list = '/api-upms/sys/files/list',
  save = '/api-upms/sys/files/add',
  edit = '/api-upms/sys/files/edit',
  getFileDetail = '/api-upms/sys/files/getFileDetail',
  delete = '/api-upms/sys/files/delete',
  deleteBatch = '/api-upms/sys/files/deleteBatch',
  getFileList = '/api-upms/sys/files/getFileList',
  recycleBinList = '/api-upms/sys/files/recycleBin',
  putRecycleBin = '/api-upms/sys/files/putRecycleBin',
  deleteRecycleBin = '/api-upms/sys/files/deleteRecycleBin',
  generateMyFile = '/api-upms/sys/files/generateMyFile',
  createAFolder = '/api-upms/sys/files/createAFolder',
  getShareFolder = '/api-upms/sys/files/getShareFolder',
  changeStar = '/api-upms/sys/files/changeStar',
  addAuthUser = '/api-upms/sys/files/addAuthUser',
  deleteAuth = '/api-upms/sys/files/deleteAuth',
  changeAuth = '/api-upms/sys/files/changeAuth',
  entrustedFolder = '/api-upms/sys/files/entrustedFolder',
  exportXls = '/api-upms/sys/files/export',
  importXls = '/api-upms/sys/files/import',
  findUsersByIds = '/api-upms/sys/user/findUsersByIds',
  getCurrentUserTenants = '/api-upms/sys/tenant/getCurrentUserTenant',
  getIsRootFolderList = '/api-upms/sys/files/getIsRootFolderList',
  getDetailLog = '/api-upms/sys/files/getDetailLog',
  deleteShareFolder = '/api-upms/sys/files/deleteShareFolder',
  uploadFile = '/api-upms/sys/files/uploadSysFile',
  searchFolder = '/api-upms/sys/files/searchFolder',
  downLoadFiles = '/api-upms/sys/files/downLoadFiles',
  copyFiles = '/api-upms/sys/files/copyFiles',
  moveFiles = '/api-upms/sys/files/moveFiles',
  renameFile = '/api-upms/sys/files/renameFile',
  incrDownCount = '/api-upms/sys/files/incrDownCount',
  incrReadCount = '/api-upms/sys/files/incrReadCount',
}

/**
 * 上传文件
 * @param params
 * @param onUploadProgress
 * @returns
 */
export function uploadFile(params: UploadFileParams, onUploadProgress: (progressEvent: ProgressEvent) => void) {
  return defHttp.uploadFile(
    {
      url: `${globSetting.domainUrl}` + Api.uploadFile,
      onUploadProgress,
    },
    params,
    { isReturnResponse: true }
  );
}

/**
 * 查询文件列表
 * @param params
 */
export const list = (params) => {
  return defHttp.get({ url: Api.list, params });
};

/**
 * 保存或者更新文件
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params }, { isTransformResponse: false });
};

/**
 * 查询文件详情
 * @param params
 */
export const getFileDetail = (params) => {
  return defHttp.get({ url: Api.getFileDetail, params });
};

/**
 * 删除文件
 * @param params
 */
export const deleteFile = (params, handleSuccess) => {
  Modal.confirm({
    title: '删除文件',
    content: '文件的引用和分享链接也将失效，确认删除？',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.delete, data: { id: params.id } }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 批量删除文件
 * @param params
 */
export const batchDeleteFile = (params, handleSuccess) => {
  Modal.confirm({
    title: '提示',
    content: '是否删除选中数据？',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 查询文件列表
 * @param params
 */
export const getFileList = (params) => {
  // 查询条件为回收站时，调用加收站接口，否则为正常的文件接口
  const url = params.isRecycleBin ? Api.recycleBinList : Api.getFileList;
  delete params.isRecycleBin;
  return defHttp.get({ url: url, params });
};

/**
 * 回收站还原
 * @param params
 */
export const putRecycleBin = (params, handleSuccess) => {
  Modal.confirm({
    title: '还原文件',
    content: '确认还原选中的文件？',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.put({ url: Api.putRecycleBin, params }).then(() => {
        handleSuccess();
      });
    },
  });
};
/**
 * 回收站删除
 * @param params
 */
export const deleteRecycleBin = (params, handleSuccess) => {
  Modal.confirm({
    title: '彻底删除文件',
    content: '文件的引用和分享链接也将失效，确认删除？',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteRecycleBin, params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 创建目录
 * @param params
 */
export const generateMyFile = (params) => {
  return defHttp.post({ url: Api.generateMyFile, params }, { isTransformResponse: false });
};

/**
 * 创建目录
 * @param params
 */
export const createAFolder = (params) => {
  return defHttp.post({ url: Api.createAFolder, params });
};

/**
 * 获得共享目录
 * @param params
 */
export const getShareFolder = (params) => {
  return defHttp.get({ url: Api.getShareFolder, params });
};

/**
 * 切换关注
 * @param params
 */
export const changeStar = (params, handleSuccess) => {
  return defHttp.post({ url: Api.changeStar, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 配置协作用户
 * @param params
 */
export const addAuthUser = (params) => {
  return defHttp.post({ url: Api.addAuthUser, params });
};

/**
 * 配置协作用户
 * @param params
 */
export const deleteAuth = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteAuth, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 变更协作用户权限
 * @param params
 */
export const changeAuth = (params) => {
  return defHttp.post({ url: Api.changeAuth, params });
};

/**
 * 目录移交
 * @param params
 */
export const entrustedFolder = (params, handleSuccess) => {
  return defHttp.post({ url: Api.entrustedFolder, params }).then(() => {
    handleSuccess();
  });
};

/**
 * 目录移交
 * @param params
 */
export const findUsersByIds = (params) => {
  return defHttp.get({ url: Api.findUsersByIds, params });
};

/**
 * 获取登录用户部门信息
 */
export const getUserTenants = (params?) => defHttp.get({ url: Api.getCurrentUserTenants, params });

/**
 * 获取1级文件夹
 */
export const getIsRootFolderList = (params?) => defHttp.get({ url: Api.getIsRootFolderList, params });

/**
 * 获得目录日志
 * @param params
 * @returns
 */
export const getDetailLog = (params?) => defHttp.get({ url: Api.getDetailLog, params });

/**
 * 删除共享目录
 * @param params
 */
export const deleteShareFolder = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteShareFolder, params }).then(() => {
    handleSuccess();
  });
};

/**
 * 查询目录
 * @param params
 */
export const searchFolder = (params) => {
  return defHttp.get({ url: Api.searchFolder, params });
};

/**
 * 删除共享目录
 * @param params
 */
export const copyFiles = (params) => {
  return defHttp.post({ url: Api.copyFiles, params });
};

/**
 * 移动共享目录
 * @param params
 */
export const moveFiles = (params) => {
  return defHttp.post({ url: Api.moveFiles, params });
};

/**
 * 文件编辑
 * @param params
 */
export const renameFile = (params) => {
  return defHttp.post({ url: Api.renameFile, params });
};

/**
 * 文件下载
 */
export const downLoadFiles = (params) => {
  return defHttp.downLoadFile({ url: Api.downLoadFiles, method: 'GET', params });
};

/**
 * 增加下截次数
 */
export const incrDownCount = (params) => {
  return defHttp.put({ url: Api.incrDownCount, params });
};

/**
 * 增加阅读次数
 */
export const incrReadCount = (params) => {
  return defHttp.put({ url: Api.incrReadCount, params });
};
